package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.FileModel;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.service.ISysCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author paul
 * @date 2023-06-19
 */
@RestController
@RequestMapping("/check")
public class SysCheckController extends BaseController
{
    @Autowired
    private ISysCheckService sysCheckService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCheck sysCheck)
    {
        startPage();
        List<SysCheck> list = sysCheckService.selectSysCheckList(sysCheck);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                List<FileModel> fileImgList = new ArrayList<>();
                if (!StringUtils.isBlank(list.get(i).getCheckImg())){
                    List<String> list1 = Arrays.asList(list.get(i).getCheckImg().split(","));
                    for (int j = 0; j < list1.size(); j++) {
                        FileModel fileModel = new FileModel();
                        fileModel.setUrl(list1.get(j));
                        fileImgList.add(fileModel);
                    }
                }
                list.get(i).setFileImgList(fileImgList);

                List<FileModel> fileVideoList = new ArrayList<>();
                if (!StringUtils.isBlank(list.get(i).getCheckVideo())){
                    List<String> list1 = Arrays.asList(list.get(i).getCheckVideo().split(","));
                    for (int j = 0; j < list1.size(); j++) {
                        FileModel fileModel = new FileModel();
                        fileModel.setUrl(list1.get(j));
                        fileVideoList.add(fileModel);
                    }
                }
                list.get(i).setFileVideoList(fileVideoList);

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:check:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCheck sysCheck)
    {
        List<SysCheck> list = sysCheckService.selectSysCheckList(sysCheck);
        ExcelUtil<SysCheck> util = new ExcelUtil<SysCheck>(SysCheck.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:check:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysCheck sysCheck = sysCheckService.selectSysCheckById(id);
        if (sysCheck != null) {
            List<FileModel> fileImgList = new ArrayList<>();
            List<FileModel> fileVideoList = new ArrayList<>();
            if (!StringUtils.isBlank(sysCheck.getCheckImg())){
                List<String> list1 = Arrays.asList(sysCheck.getCheckImg().split(","));
                for (int j = 0; j < list1.size(); j++) {
                    FileModel fileModel = new FileModel();
                    //response.url.substring(response.url.lastIndexOf('/') + 1, response.url.lastIndexOf('.')),
                    fileModel.setName(list1.get(j).substring(list1.get(j).lastIndexOf('/') + 1, list1.get(j).lastIndexOf('.')));
                    fileModel.setUrl(list1.get(j));
                    fileImgList.add(fileModel);
                }

                if (!StringUtils.isBlank(sysCheck.getCheckVideo())){
                    List<String> list2 = Arrays.asList(sysCheck.getCheckVideo().split(","));
                    for (int m = 0; m < list2.size(); m++) {
                        FileModel fileModel = new FileModel();
                        fileModel.setName(list2.get(m).substring(list2.get(m).lastIndexOf('/') + 1, list2.get(m).lastIndexOf('.')));
                        fileModel.setUrl(list2.get(m));
                        fileVideoList.add(fileModel);
                    }
                }
            }
            sysCheck.setFileImgList(fileImgList);
            sysCheck.setFileVideoList(fileVideoList);
        }
        return success(sysCheck);
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:check:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCheck sysCheck)
    {
        return toAjax(sysCheckService.insertSysCheck(sysCheck));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:check:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCheck sysCheck)
    {
        if (sysCheck != null) {
            List<FileModel> fileImgList = new ArrayList<>();
            if (!StringUtils.isBlank(sysCheck.getCheckImg())){
                List<String> list1 = Arrays.asList(sysCheck.getCheckImg().split(","));
                for (int j = 0; j < list1.size(); j++) {
                    FileModel fileModel = new FileModel();
                    fileModel.setUrl(list1.get(j));
                    fileImgList.add(fileModel);
                }
            }
            sysCheck.setFileImgList(fileImgList);
        }
        return toAjax(sysCheckService.updateSysCheck(sysCheck));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:check:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysCheckService.deleteSysCheckByIds(ids));
    }
}
